package com.zshan.clinic.admin.service.stat;


import com.zshan.clinic.admin.entity.vo.StatClinicRegVo;
import com.zshan.clinic.admin.mapper.stat.SpecStatMapper;
import com.zshan.clinic.database.dao.StatDoctorRegisterDailyMapper;
import com.zshan.clinic.database.model.StatDoctorRegisterDaily;
import com.zshan.clinic.database.model.StatDoctorRegisterDailyExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 医生挂号统计
 * @author lichen
 */

@Service
@Slf4j
public class DoctorRegDailyService {


    @Autowired
    private StatDoctorRegisterDailyMapper statDoctorRegisterDailyMapper;

    @Autowired
    private SpecStatMapper specStatMapper;

    /**
     * 诊所当日挂号统计
     * @param clinicId
     * @param visitDate
     * @return
     */
    public StatClinicRegVo getRegStat(Long clinicId, LocalDate visitDate,Long doctorId) {
        return specStatMapper.getRegStat(clinicId,visitDate,doctorId);
    }

    /**
     * 查询医生挂号统计
     * @param doctorId
     * @param statDate
     * @return
     */
    public StatDoctorRegisterDaily getStatDoctorById(Long clinicId,Long doctorId, LocalDate statDate){
        StatDoctorRegisterDailyExample example = new StatDoctorRegisterDailyExample();
        StatDoctorRegisterDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        criteria.andDoctorIdEqualTo(doctorId);
        criteria.andStatDateEqualTo(statDate);
        List<StatDoctorRegisterDaily> dailyList = statDoctorRegisterDailyMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(dailyList)){
            return null;
        }
        return dailyList.get(0);
    }

    /**
     * 保存医生挂号统计
     * @param statDoctorRegisterDaily
     */
    public void saveOrUpdate(StatDoctorRegisterDaily statDoctorRegisterDaily){
        StatDoctorRegisterDaily dbStatDoctorRegisterDaily = getStatDoctorById(statDoctorRegisterDaily.getClinicId(),statDoctorRegisterDaily.getDoctorId(),statDoctorRegisterDaily.getStatDate());
        if (dbStatDoctorRegisterDaily == null) {
            if (statDoctorRegisterDaily.getReservedCount() == null) {
                statDoctorRegisterDaily.setReservedCount(0);
            }
            if (statDoctorRegisterDaily.getRegisterCount() == null) {
                statDoctorRegisterDaily.setRegisterCount(0);
            }
            if (statDoctorRegisterDaily.getCheckinCount() == null) {
                statDoctorRegisterDaily.setCheckinCount(0);
            }
            if (statDoctorRegisterDaily.getDiagnosedCount() == null) {
                statDoctorRegisterDaily.setDiagnosedCount(0);
            }
            if (statDoctorRegisterDaily.getRefundedCount() == null) {
                statDoctorRegisterDaily.setRefundedCount(0);
            }
            if (statDoctorRegisterDaily.getExpiredCount() == null) {
                statDoctorRegisterDaily.setExpiredCount(0);
            }
            statDoctorRegisterDaily.setCreateTime(new Date());
            statDoctorRegisterDailyMapper.insertSelective(statDoctorRegisterDaily);
        } else {
            if (statDoctorRegisterDaily.getReservedCount() != null) {
                dbStatDoctorRegisterDaily.setReservedCount(dbStatDoctorRegisterDaily.getReservedCount() + statDoctorRegisterDaily.getReservedCount());
            }
            if (statDoctorRegisterDaily.getRegisterCount() != null) {
                dbStatDoctorRegisterDaily.setRegisterCount(dbStatDoctorRegisterDaily.getRegisterCount() + statDoctorRegisterDaily.getRegisterCount());
            }
            if (statDoctorRegisterDaily.getCheckinCount() != null) {
                dbStatDoctorRegisterDaily.setCheckinCount(dbStatDoctorRegisterDaily.getCheckinCount() + statDoctorRegisterDaily.getCheckinCount());
            }
            if (statDoctorRegisterDaily.getDiagnosedCount() != null) {
                dbStatDoctorRegisterDaily.setDiagnosedCount(dbStatDoctorRegisterDaily.getDiagnosedCount() + statDoctorRegisterDaily.getDiagnosedCount());
            }
            if (statDoctorRegisterDaily.getRefundedCount() != null) {
                dbStatDoctorRegisterDaily.setRefundedCount(dbStatDoctorRegisterDaily.getRefundedCount() + statDoctorRegisterDaily.getRefundedCount());
            }
            if (statDoctorRegisterDaily.getExpiredCount() != null) {
                dbStatDoctorRegisterDaily.setExpiredCount(dbStatDoctorRegisterDaily.getExpiredCount() + statDoctorRegisterDaily.getExpiredCount());
            }
            dbStatDoctorRegisterDaily.setUpdateTime(new Date());
            statDoctorRegisterDailyMapper.updateByPrimaryKeySelective(dbStatDoctorRegisterDaily);
        }
    }
}
